package com.dong.course;

import com.dong.course.filter.CourseLoginFilter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

@SpringBootApplication
public class CourseEdgeApp implements ApplicationContextAware {
    public static void main(String[] args) {
        new SpringApplication(CourseEdgeApp.class).run(args);
    }
    private ApplicationContext applicationContext;
    @Bean
    public FilterRegistrationBean filterRegistrationBean () {
        FilterRegistrationBean register = new FilterRegistrationBean();

        CourseLoginFilter filter = applicationContext.getBean(CourseLoginFilter.class);

        register.setFilter(filter);
        register.setUrlPatterns(Arrays.asList(new String[] {"/*"}));
        return register;

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
